package org.bookie.web.pages;

import javax.mail.MessagingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.engine.ILink;
import org.bookie.core.services.IUserManager;
import org.bookie.framework.CommonBasePage;

/**
 * Page that sends to the user his/her new password
 * 
 * @author igor, zubairov
 * 
 */
public abstract class ForgotPassword extends CommonBasePage {
	/**
	 * Logger for this class
	 */
	private static final Log log = LogFactory.getLog(ForgotPassword.class);

	@InjectObject("service:bookie.core.UserManager")
	public abstract IUserManager getUserManager();

	public abstract String getUsername();

	public abstract String getEmail();
	
	/**
	 * Main listener method
	 * 
	 * @param cycle
	 * @return
	 * @throws MessagingException 
	 */
	public ILink createPassword(IRequestCycle cycle) throws MessagingException {
		log.debug("User initiated forgot password procedure");
		if (getDelegate().getHasErrors())
			return null;
		boolean result = getUserManager().recreatePassword(getUsername(), getEmail());
		if (!result) {
			recordError("forgotPassword.form.failure");
			return null;
		}
		setMessage(getMessages().format("forgotPassword.success.message", getEmail()));
		return getPageService().getLink(false, getPageName());
	}
}
